Chapter 26 THE CONDITIONAL RENDEZVOUS IMPATIENT PROGRAMMING _________________________________________________________________ Many times in your life you wish to do something but you decide after trying for a short period of time, that it cannot be done, so you change your mind and do something else. This capability must be available in a computer program also, since many times a computer program is used to simulate something in real life. This chapter will give examples of impatient programs, those that refuse to simply wait forever for a certain job to be done. THE BASIC PROGRAM OUTLINE _________________________________________________________________ Examine the file named MEALS1.ADA for the ================ program that will serve as the outline for the MEALS1.ADA selective rendezvous even though it does not ================ contain one. The program consists of four tasks, one each to describe Bill's day and John's day, and one each to describe eating a meal at a restaurant and at a fast food Burger Boy (hopefully not an actual restaurant). In this program, all of the delay times have been defined in terms of a constant named HOURS which actually causes a delay of one second per hour to speed up program execution to a reasonable level. Eating a meal at the restaurant requires half an hour to be served and another half hour to consume the meal, whereas the Burger Boy requires only one tenth of an hour to be served and another tenth to consume the meal. The task named Bills_Day has him eating three meals only one hour apart, and the task named Johns_Day has him eating two meals in rapid succession then waiting four hours until he eats supper. Another interesting point is the fact that both eat every meal at the restaurant with no concern for how long they have to wait to be seated and eat. It is of no concern either to the Ada program at hand or the analysis of the problem, that one of the tasks is never called, because this is perfectly legal. Finally, both the restaurant and the Burger Boy can only serve one customer at a time, since this is the way the tasks were programmed. They can however, each serve a different customer at the same time. EATING MEALS IS VERY SLOW _________________________________________________________________ You will notice, by inspecting the delays, that John waits 0.4 hours and gets to the restaurant first, then takes a total of one Page 26-1 Chapter 26 - The Conditional Rendezvous hour to eat. Bill waits one hour, and goes to the restaurant, but must wait on the entry queue for 0.4 hours for John to finish eating, then takes another hour to eat. During this hour, John has returned and is waiting to be served again. They continue getting in each others way and finally each consumes three meals. DEADLOCK OCCURS _________________________________________________________________ The observant student will notice that the tasks named Bills_Day and Johns_Day each run through sequentially to completion and make no additional calls. The two named Restaurant and Burger_Boy however, each consist of an infinite loop and never actually complete. When the first two tasks execute to completion, the system will recognize that there are no tasks running that are capable of making calls to the waiting accept statements. The system will recognize this condition, and it will terminate the program due to deadlock after displaying a message that deadlock has occurred. We still have not given an acceptable method of terminating our program gracefully, but we will later in this chapter. You should spend the time necessary to thoroughly understand this program since it will be the basis for the remaining example programs in this chapter. When you do understand it, compile and execute it to see if your compiler will recognize the deadlock condition. THE CONDITIONAL RENDEZVOUS _________________________________________________________________ Examine the program named MEALS2.ADA for our ================ first example of a conditional rendezvous. This MEALS2.ADA program is identical to the last one named ================ MEALS1.ADA except for the addition of a few statements in the task named Bills_Day. In this program Bill is in a bit of a hurry to eat his first two meals and puts in a few conditions to indicate this. THE SELECTED RENDEZVOUS _________________________________________________________________ When Bill starts his day, he is very impatient and will not wait at all for his first meal. If the restaurant is not ready to serve a meal immediately, he will go to the Burger Boy and have his breakfast. The statements in lines 29 through 33 replace the single statement in line 29 of the last program to indicate this impatience. In Ada terms, if the task named Restaurant is not waiting at the Eat_A_Meal entry point, the call will not be made there, but a call will be made to the entry point named Eat_A_Meal in the Burger_Boy task. If the Burger_Boy task is not waiting at Page 26-2 Chapter 26 - The Conditional Rendezvous the entry point, then Bill will be required to wait until it is ready, when he will be served. The reserved word else is used to indicate the selected rendezvous and it can follow as many or clauses as desired. The general form is; select ; or ; else ; end select; The else clause will be taken if none of the other entry points are waiting at their respective rendezvous points. THE DELAYED RENDEZVOUS _________________________________________________________________ Bill is not quite as hungry for his second meal so he is willing to wait for a short period at the restaurant, but if he is not served within one tenth of an hour, he will go to the Burger Boy for lunch. The single statement in lines 35 through 40 replaces the statement in line 31 of the previous program. In Ada terms, the task named Bills_Day will wait .1 hour for the Restaurant task to reach its entry point, after which it will call the entry point of Burger_Boy and wait no matter how long it takes to be served there. As many selections as desired can be used, with delay statements in as many of the selections as needed. The general form is given by; select delay